﻿using System.ServiceModel;
using Microsoft.Practices.Unity.InterceptionExtension;
using SpectrumAnalyser.Services.Common.Extensions;
using SpectrumAnalyser.Services.ExpertiseResultStorageServiceContracts;
using SpectrumAnalyser.Common.ServiceHeaders;

namespace SpectrumAnalyser.Services.Common.CallHandlers
{
    public class RequiresInstanceNameCallHandler : ICallHandler
    {
        public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
        {
            if (input.Target is IExpertiseStorageServiceContract)
            {
                var instanceNameHeader = OperationContext.Current.GetInstanceNameHeader();

                (input.Target as IExpertiseStorageServiceContract).Instance = instanceNameHeader.InstanceName;
            }
            return getNext()(input, getNext);
        }

        public int Order { get; set; }
    }
}